package cn.rongdao.jrkabao.ui.remind;

import android.content.Context;
import android.support.v4.content.ContextCompat;
import android.support.v7.widget.LinearLayoutManager;
import android.view.View;

import com.mifengkong.frtools.http.request.callback.FRBaseObserver;
import com.mifengkong.frtools.ui.activity.FRStartActivity;
import com.mifengkong.frtools.ui.adapter.recyclerview.FRBaseRecyclerAdapter;
import com.mifengkong.frwidget.recyclerview.PtrRecyclerViewUIComponent;
import com.mifengkong.frwidget.recyclerview.divider.RecyclerViewDivider;
import com.mifengkong.frwidget.refersh.OnPullToRefreshListener;
import com.mifengkong.frwidget.titlebar.TitleBarUIComponent;

import org.greenrobot.eventbus.EventBus;

import java.util.List;

import butterknife.BindView;
import cn.rongdao.jrkabao.R;
import cn.rongdao.jrkabao.base.BaseAutoFinishActivity;
import cn.rongdao.jrkabao.bean.remind.RemindMailListBean;
import cn.rongdao.jrkabao.model.remind.RemindMailListModel;
import cn.rongdao.jrkabao.ui.remind.adapter.RemindMailListAdapter;
import cn.rongdao.jrkabao.ui.remind.adapter.RemindMailListViewHolder;
import cn.rongdao.jrkabao.ui.remind.event.MoXieSuccessByUpdateEvent;
import cn.rongdao.jrkabao.util.moxie.MoXieManager;

/**
 * Created by jack on 2017/11/24
 */

public class RemindMailListActivity extends BaseAutoFinishActivity implements FRBaseRecyclerAdapter.OnItemClickListener<RemindMailListViewHolder> {

    public static void start(Context context) {
        FRStartActivity.start(context, RemindMailListActivity.class);
    }

    @BindView(R.id.arml_titlebar)
    TitleBarUIComponent titleBarUIComponent;
    @BindView(R.id.arml_ptr_framelayout)
    PtrRecyclerViewUIComponent ptrRecyclerViewUIComponent;

    private RemindMailListAdapter mAdapter;
    private MoXieManager moXieManager;

    @Override
    protected int layoutRes() {
        return R.layout.activity_remind_mail_list;
    }

    @Override
    protected void initView() {
        super.initView();

        initTitleBar();
        initPtr();
        initMoXieListener();
    }

    private void initPtr() {
        moXieManager = new MoXieManager(this);
        moXieManager.initKaMaoMoXie();
        mAdapter = new RemindMailListAdapter(this, this);
        ptrRecyclerViewUIComponent.setLayoutManager(new LinearLayoutManager(getActivity()));
        ptrRecyclerViewUIComponent.setRecyclerViewDivider(new RecyclerViewDivider(getActivity()));
        ptrRecyclerViewUIComponent.setAdapter(mAdapter);
        ptrRecyclerViewUIComponent.delayRefresh();
        ptrRecyclerViewUIComponent.setOnPullToRefreshListener(new OnPullToRefreshListener() {
            @Override
            public void onPullToRefresh() {
                getMailData();
            }
        });
    }

    private void getMailData() {
        RemindMailListModel.getInstance().execute(new FRBaseObserver<List<RemindMailListBean>>(this) {
            @Override
            protected void onBaseNext(List<RemindMailListBean> data) {
                mAdapter.setDataList(data);
                ptrRecyclerViewUIComponent.refreshComplete();
            }

            @Override
            protected void onBaseError(Throwable t) {
                super.onBaseError(t);
                ptrRecyclerViewUIComponent.refreshComplete();
            }
        });
    }

    private void initMoXieListener() {
        moXieManager.setOnMoXieReturnListener(new MoXieManager.OnMoXieReturnListener() {
            @Override
            public void looperMethod(String email) {
                //TODO 轮询
            }

            @Override
            public void successReturn(String email) {
                //TODO 做一些成功的事情
                moXieManager.onStop();
                EventBus.getDefault().post(new MoXieSuccessByUpdateEvent(email));
                moXieManager = null;
                finish();
            }
        });
    }

    private void initTitleBar() {
        titleBarUIComponent.setBackgroundColor(ContextCompat.getColor(thisActivity(), R.color.colorPrimary));
        titleBarUIComponent.initTitle("邮箱登录");
        titleBarUIComponent.initLeftBack(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
    }

    @Override
    public void onItemClick(RemindMailListViewHolder remindMailListViewHolder, int position) {
        String jumpEmail = mAdapter.getDataList().get(position).getEmail();
        moXieManager.startMoXieWithEmail(jumpEmail);
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        moXieManager = null;
    }
}
